package com.swss.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8954975565748986814L;

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		// TODO Auto-generated method stub
		ActionContext actionContext = actionInvocation.getInvocationContext();
		Map session = actionContext.getSession();
		// except login action
		/*
		 * Object action = actionInvocation.getAction(); if (action instanceof
		 * AdminLoginAction) { return actionInvocation.invoke(); }
		 */
		// check session
		HttpServletRequest request = ServletActionContext.getRequest();

		if (session.get("user") == null
				&& request.getRequestURI().indexOf("/swss/user/login.action") == -1) {

			HttpSession ses = request.getSession();
			String loginMsg = "登录超时，请重新登录";
			ses.setAttribute("loginMsg", loginMsg);
			return "login";
		}
		return actionInvocation.invoke();// go on
	}

}
